import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import '../../../core/platform/platform_detector.dart';

/// 响应式图表组件
class ResponsiveChart extends StatelessWidget {
  final List<FlSpot> data;
  final Color barColor;
  final String title;
  final double? height;
  final bool showTitles;

  const ResponsiveChart({
    super.key,
    required this.data,
    this.barColor = Colors.blue,
    this.title = '',
    this.height,
    this.showTitles = true,
  });

  @override
  Widget build(BuildContext context) {
    // 根据平台调整图表高度
    final chartHeight =
        height ?? (PlatformDetector.isDesktop() ? 300.0 : 200.0);

    return Container(
      height: chartHeight,
      padding: PlatformDetector.isDesktop()
          ? const EdgeInsets.all(16)
          : const EdgeInsets.all(12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (title.isNotEmpty) ...[
            SelectableText(
              title,
              style: Theme.of(context).textTheme.titleMedium?.copyWith(
                    fontWeight: FontWeight.bold,
                  ),
            ),
            const SizedBox(height: 12),
          ],
          Expanded(
            child: LineChart(
              _buildLineChartData(context),
            ),
          ),
        ],
      ),
    );
  }

  LineChartData _buildLineChartData(BuildContext context) {
    return LineChartData(
      gridData: FlGridData(
        show: true,
        drawVerticalLine: true,
        horizontalInterval: 1,
        verticalInterval: 1,
        getDrawingHorizontalLine: (value) {
          return FlLine(
            color: Colors.grey.withOpacity(0.2),
            strokeWidth: 1,
          );
        },
        getDrawingVerticalLine: (value) {
          return FlLine(
            color: Colors.grey.withOpacity(0.2),
            strokeWidth: 1,
          );
        },
      ),
      titlesData: FlTitlesData(
        show: showTitles,
        rightTitles: AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        bottomTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: showTitles,
            reservedSize: PlatformDetector.isDesktop() ? 30 : 20,
            interval: 1,
            getTitlesWidget: (value, meta) {
              if (value.toInt() >= 0 && value.toInt() < data.length) {
                return SideTitleWidget(
                  axisSide: meta.axisSide,
                  child: SelectableText(
                    '${value.toInt() + 1}',
                    style: TextStyle(
                      fontSize: PlatformDetector.isDesktop() ? 12 : 10,
                      color: Colors.grey,
                    ),
                  ),
                );
              }
              return const SelectableText('');
            },
          ),
        ),
        leftTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: showTitles,
            reservedSize: PlatformDetector.isDesktop() ? 40 : 30,
            interval: 1,
            getTitlesWidget: (value, meta) {
              return SideTitleWidget(
                axisSide: meta.axisSide,
                child: SelectableText(
                  '${value.toInt()}',
                  style: TextStyle(
                    fontSize: PlatformDetector.isDesktop() ? 12 : 10,
                    color: Colors.grey,
                  ),
                ),
              );
            },
          ),
        ),
      ),
      borderData: FlBorderData(
        show: true,
        border: Border.all(color: Colors.grey.withOpacity(0.2)),
      ),
      minX: 0,
      maxX: data.length.toDouble() - 1,
      minY: 0,
      maxY: _getMaxYValue(),
      lineBarsData: [
        LineChartBarData(
          spots: data,
          isCurved: true,
          color: barColor,
          barWidth: PlatformDetector.isDesktop() ? 3 : 2,
          isStrokeCapRound: true,
          dotData: FlDotData(
            show: true,
            getDotPainter: (spot, percent, barData, index) {
              return FlDotCirclePainter(
                radius: PlatformDetector.isDesktop() ? 4 : 3,
                color: barColor,
                strokeWidth: 0,
              );
            },
          ),
          belowBarData: BarAreaData(
            show: true,
            color: barColor.withOpacity(0.3),
          ),
        ),
      ],
    );
  }

  double _getMaxYValue() {
    if (data.isEmpty) return 1;
    double max = data.first.y;
    for (var spot in data) {
      if (spot.y > max) max = spot.y;
    }
    // 添加一些顶部空间
    return max * 1.2;
  }
}
